#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @Time    : 2022/4/17 1:49 下午
# @Author  : WangZhixing
import os
import json
path = "/Users/wzx/Downloads/module-reverse-by-gnn/统计文件覆盖问题/Bash"

filesymbol = "/Users/wzx/Downloads/module-reverse-by-gnn/统计文件覆盖问题/filedefsymbol.json"

filepath = "/Users/wzx/Downloads/research/bash-4.2/"

def sourcefilenamelist(filepath):
    sourcelist = []
    for root, dirs, files in os.walk(filepath):
        for name in files:
            if not name.startswith('.'):
                if name.endswith(".c") or name.endswith(".h"):
                    filename = os.path.join(root,name)
                    filename = filename.replace(filepath,"")
                    sourcelist.append(filename)
    return sourcelist

sourcelist = sourcefilenamelist(filepath)



def compare(a,b):
    filename=[]
    with open(b,"r") as fp:
        jsonfiles = json.load(fp)
        for jsonfile in jsonfiles:
            if jsonfile['name']not in filename:
                filename.append(jsonfile['name'])

    edgefilename = []
    with open(a, "r") as fp:
        for lines in fp.readlines():

            line = lines.split("\n")[0]
            line = line.split()
            if line[1] not in edgefilename:
                edgefilename.append(line[1])
            if line[2]not in edgefilename:
                edgefilename.append(line[2])
    notin = []

    for file in edgefilename:
        if file not in filename and file in sourcelist:
            notin.append(file)

    print(len(notin))
    print(notin)

for method in os.listdir(path):
    if not method.startswith("."):
        metgodpath = os.path.join(path,method)
        print(method)
        for file in os.listdir(metgodpath):
            if not file.startswith("."):
                if file.endswith(".rsf"):
                    filepath = os.path.join(metgodpath,file)
                    compare(filepath,filesymbol)
